package com.java46.controller;

import com.java46.bean.JsonBean;
import com.java46.bean.Personnel;
import com.java46.exception.MyException;
import com.java46.service.PersonnelService;
import com.java46.util.ControllerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class PersonnelController {

    @Autowired
    private PersonnelService personnelService;

    /**
     * 查询所有员工信息
     * @return 返回json
     */
    @RequestMapping("getAllPersonnel")
    @ResponseBody
    public JsonBean getAllPer(){

        List<Personnel> allPersonnel = null;
        try {
            //执行service的查询方法
            allPersonnel = personnelService.getAllPersonnel();
        } catch (MyException e) {
            e.printStackTrace();
            // 失败时输入异常，并封装错误信息，return
            return ControllerUtil.setFail(e.getErrorMsg());
        }
        // 成功后自动封装正确数据，return
        return ControllerUtil.setSuccess(allPersonnel);
    }

    @RequestMapping(value = "addPersonnel")
    public void addPersonnel(Personnel personnel){
        personnelService.addPersonnel(personnel);
    }
}
